package cn.demo.act.service.impl;

import java.util.List;

import cn.demo.common.utils.SecurityUtils;
import cn.demo.common.utils.StringUtils;
import cn.demo.um.domain.UmClientUser;
import cn.demo.um.mapper.UmClientUserMapper;
import com.mybatisflex.core.paginate.Page;
import cn.demo.common.core.page.PageQuery;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.demo.act.mapper.ActActivityInfoMapper;
import cn.demo.act.domain.ActActivityInfo;
import cn.demo.act.service.IActActivityInfoService;

import static cn.demo.act.domain.table.ActRegisterTableDef.ACT_REGISTER;

/**
 * 活动管理Service业务层处理
 *
 * @author xtjun
 * @date 2024-06-06
 */
@Service
public class ActActivityInfoServiceImpl extends ServiceImpl<ActActivityInfoMapper, ActActivityInfo> implements IActActivityInfoService {
    @Resource
    private ActActivityInfoMapper actActivityInfoMapper;
    @Resource
    private UmClientUserMapper umClientUserMapper;
    /**
     * 查询活动管理
     *
     * @param id 活动管理主键
     * @return 活动管理
     */
    @Override
    public ActActivityInfo selectActActivityInfoById(String id) {
        ActActivityInfo actActivityInfo = actActivityInfoMapper.selectOneById(id);
        QueryWrapper qw = QueryWrapper.create().in(UmClientUser::getId, QueryWrapper.create().from(ACT_REGISTER).select(ACT_REGISTER.USER_ID).and(ACT_REGISTER.ACTIVITY_ID.eq(id)));
        actActivityInfo.setParticipants(umClientUserMapper.selectListWithRelationsByQuery(qw));
        return actActivityInfo;
    }

    /**
     * 查询活动管理列表
     *
     * @param actActivityInfo 活动管理
     * @return 活动管理
     */
    @Override
    public Page<ActActivityInfo> selectActActivityInfoPage(ActActivityInfo actActivityInfo, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create(actActivityInfo, ActActivityInfo.operators).orderBy(pageQuery.getOrderByColumn(), pageQuery.isAsc());
        if (null != actActivityInfo.getParams().get("isMine") && "true".equals(actActivityInfo.getParams().get("isMine").toString())){
            queryWrapper.in(ActActivityInfo::getId, QueryWrapper.create().select(ACT_REGISTER.ACTIVITY_ID).from(ACT_REGISTER).where(ACT_REGISTER.USER_ID.eq(SecurityUtils.getUserId())));
        }
        return actActivityInfoMapper.paginate(pageQuery.build(), queryWrapper);
    }

    @Override
    public List<ActActivityInfo> selectActActivityInfoList(ActActivityInfo actActivityInfo) {
        return actActivityInfoMapper.selectListByQuery(QueryWrapper.create(actActivityInfo, ActActivityInfo.operators));
    }

    /**
     * 新增活动管理
     *
     * @param actActivityInfo 活动管理
     * @return 结果
     */
    @Override
    public int insertActActivityInfo(ActActivityInfo actActivityInfo) {
        return actActivityInfoMapper.insert(actActivityInfo);
    }

    /**
     * 修改活动管理
     *
     * @param actActivityInfo 活动管理
     * @return 结果
     */
    @Override
    public int updateActActivityInfo(ActActivityInfo actActivityInfo) {
        return actActivityInfoMapper.update(actActivityInfo);
    }

    /**
     * 批量删除活动管理
     *
     * @param ids 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteActActivityInfoByIds(List<String> ids) {
        return actActivityInfoMapper.deleteBatchByIds(ids);
    }

    /**
     * 删除活动管理信息
     *
     * @param id 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteActActivityInfoById(String id) {
        return actActivityInfoMapper.deleteById(id);
    }
}
